﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.IO;

namespace TfsBacklogMgr.Main
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private void App_Startup(object sender, StartupEventArgs e)
        {
            string fname = "";

            // look on command-line for settings filename
            if( e.Args != null && e.Args.Length > 0 )
                Properties["Filename"] = fname = e.Args[0];

            // double-click on file in shell activations don't appear on command line, but here
            if (AppDomain.CurrentDomain != null && 
                AppDomain.CurrentDomain.SetupInformation != null && 
                AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null &&
                AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null &&
                AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
            {
                try
                {
                    fname = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
                    // It comes in as a URI; this helps to convert it to a path.
                    Uri uri = new Uri(fname);
                    fname = uri.LocalPath;
                    Properties["Filename"] = fname;
                }
                catch
                {
                    // For some reason, this couldn't be read as a URI.
                    // Never mind
                }
            }
            if( ! string.IsNullOrEmpty(fname) &&
                File.Exists(fname) )
                Context.LoadFrom(fname);
        }
    }
}
